﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RegexLib.Web.Models.Data;
using RegexLib.Web.Models.Web;

namespace RegexLib.Web.Controllers
{
	public class UserController : Controller
	{
		/// <summary>
		/// 访问用户个人首页。
		/// </summary>
		/// <returns></returns>
		public ActionResult Index(string accessKey, int pageIndex, int pageSize)
		{
			using (RxDataContext db = new RxDataContext())
			{
				UserInfo user = db.UserInfos.FirstOrDefault(u => u.AccessKey == accessKey);

				if (user == null)
					return Redirect("/Home/");

				int totalPages;
				ViewData["Regexes"] = user.GetUserRegex(pageIndex, pageSize, out totalPages);
				ViewData["Keywords"] = user.GetUsedKeywords();
				ViewData["PageIndex"] = pageIndex > 0 ? pageIndex : totalPages;
				ViewData["PageSize"] = pageSize;
				ViewData["TotalPages"] = totalPages;
				ViewData["User"] = user;

				return View("UserIndex");
			}
		}

		/// <summary>
		/// 访问用户贡献页。
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public ActionResult Contributions(int pageIndex, int pageSize)
		{
			int totalPages;

			ViewData["Contributions"] = RxLibrary.GetUserContribution(pageIndex, pageSize, out totalPages);
			ViewData["PageIndex"] = pageIndex;
			ViewData["PageSize"] = pageSize;
			ViewData["TotalPages"] = totalPages;

			return View();
		}
	}
}
